using HttpProxy.Models;
using System;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web.Http;

namespace HttpProxy.Controllers
{
    /// <summary>
    /// 文件上传
    /// </summary>
    public class JsonController : ApiController
  {
    /// <summary>
    /// 上传文件夹，Web.config中配置
    /// </summary>
    public static string JsonFolder = ConfigurationManager.AppSettings["JsonFolder"];
    /// <summary>
    /// JSON更新
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost, Route("api/file/UpdateJson")]
    public bool UpdateJson(JsonFileUpdate model)
    {
      try
      {
        var jsonPath = $"{JsonFolder}\\{model.FileName}.json";
        using (StreamWriter sw = new StreamWriter(jsonPath, false, Encoding.UTF8))
        {
          sw.Write(model.JsonString);
        }
        return true;
      }
      catch (Exception ex)
      {
        return false;
      }
    }
  }
}
